Crate display_tree

source ·
Expand description

display_tree provides simple, automatic, and customizable tree pretty-printing.

This crate provies the DisplayTree trait and a macro to derive it for structs and enums. The derived implementation can be heavily customized using helper attributes discussed in the DisplayTree documentation It also provides AsTree and a set of macros mirroring standard library counterparts for displaying or formatting tree types. The way a tree is formatted can be customized with the Style type, or builder methods on AsTree, using the StyleBuilder trait.

See the DisplayTree documentation to learn how to make a type printable as a tree, or AsTree or any of the macros provided by display_tree for information on displaying or formatting your new tree type.

Examples

use display_tree::{format_tree, CharSet, DisplayTree, Style, StyleBuilder};

// A tree representing a numerical expression.
#[derive(DisplayTree)]
enum Expr {
    Int(i32),
    BinOp {
        #[node_label]
        op: char,
        #[tree]
        left: Box<Self>,
        #[tree]
        right: Box<Self>,
    },
    UnaryOp {
        #[node_label]
        op: char,
        #[tree]
        arg: Box<Self>,
    },
}

let expr: Expr = Expr::BinOp {
    op: '+',
    left: Box::new(Expr::UnaryOp {
        op: '-',
        arg: Box::new(Expr::Int(2)),
    }),
    right: Box::new(Expr::Int(7)),
};

assert_eq!(
    format_tree!(
        expr,
        Style::default()
            .indentation(1)
            .char_set(CharSet::DOUBLE_LINE)
    ),
    concat!(
        "+\n",
        "╠═ -\n",
        "║  ╚═ Int\n",
        "║     ╚═ 2\n",
        "╚═ Int\n",
        "   ╚═ 7",
    ),
);

Modules

A module containing the ToDisplayTreeRef trait and implementations for std types.

Macros

Creates a String from a type that implements DisplayTree, formatting it as a tree.
Prints a type that implements DisplayTree to the standard output as a tree.
Prints a type that implements DisplayTree to the standard output as a tree, with a newline.
Writes a type that implements DisplayTree to a buffer as a tree.
Writes a type that implements DisplayTree to a buffer as a tree, with a newline.

Structs

A helper struct for formatting a type that implements DisplayTree.
A set of chars used for formatting a type that implements DisplayTree.
A type that describes the way a type that implements DisplayTree should be formatted.
A type that described how text will be rendered.

Enums

An ANSI color that a tree can be styled with.

Traits

A type that can be pretty-printed as a tree with a specified style.
A trait that provides builder methods for constructing an instance of Style.

Derive Macros

Derive marco for the DisplayTree trait.